<template>
    <div class="transaction">

        <!-- 贴顶导航 -->
        <UserFixedHead title="Transaction record" />

        <!-- 初始化 -->
        <UxIniting v-if="!inited" />

        <template v-else>

            <h2>Transaction record</h2>

            <div class="transaction-tab" v-if="is_wallet == 2">
                <button :class="{ active: tab == 'order' }" @click="setTab('order')">Order details</button>
                <button :class="{ active: tab == 'wallet' }" @click="setTab('wallet')">Wallet details</button>
            </div>

            <div class="transaction-form">
                <UserSearchName v-model="form.order_code" size="small" shape="plain" placeholder="Order ID"
                                @change="doSearch()" />

                <!-- 时间 -->
                <UxDatepicker :start.sync="form.time_s" :end.sync="form.time_e" placeholder="Date"
                              @change="doSearch()" />

                <!-- 交易类型 -->
                <UxSelect v-model="form.trade_type" placeholder="Transaction types" :options="tradeOptions"
                          @change="doSearch()" />

                <!-- 支付方式 -->
                <UxSelect v-model="form.payment" v-if="tab == 'order'" placeholder="Payment methods"
                          :options="paymentOptions" @change="doSearch()" />

                <!-- <a href="#">Export</a> -->

                <strong><i class="iconfont icon-xiangqing1"></i>Currency unit USD</strong>
            </div>

            <div class="transaction-table">
                <table v-if="tab == 'order'">
                    <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>Transaction date</th>
                            <th>Transaction type</th>
                            <th>Payment method</th>
                            <th>Amount</th>
                            <th>Transaction status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="item in tableList" :key="item.id">
                            <td v-text="item.order"></td>
                            <td v-text="getTimeByDate(item.created_at)"></td>
                            <td v-text="getTradeById(item.trade_type)"></td>
                            <td v-text="getPaymentById(item.payment)"></td>
                            <td v-text="getPriceFormat(item.trade_sum)"></td>
                            <td v-html="getStatusHtml(item)"></td>
                        </tr>
                    </tbody>
                </table>
                <table v-else>
                    <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>Transaction date</th>
                            <th>Transaction types</th>
                            <th>Amount</th>
                            <th>Balance</th>
                            <th>Transaction status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="item in tableList" :key="item.id">
                            <td v-text="item.order_code || '---'"></td>
                            <td v-text="getTimeByDate(item.created_at)"></td>
                            <td v-text="getTradeById(item.trade_type)"></td>
                            <td v-text="getPriceFormat(item.trade_sum)"></td>
                            <!-- 支付中的不显示余额 -->
                            <td v-if="item.transaction_status == 1 || item.transaction_status == 2">--</td>
                            <td v-else v-text="getPriceFormat(item.balance)"></td>
                            <td v-html="getStatusHtml(item)"></td>
                        </tr>
                    </tbody>
                </table>

                <div class="table-nodata" v-show="tableList.length == 0">
                    <UxSearchEmpty v-if="searching" />
                    <template v-else>
                        <svg-icon name="notify" />
                        <span>NO transaction record</span>
                    </template>
                </div>
            </div>
            <!-- 分页 -->
            <CommonPage v-show="form.total > form.page_size" :current-page="form.page_num" :page-size="form.page_size"
                        :total="form.total" @current-change="handleCurrentChange"></CommonPage>

            <UxLoading2 :loading="loading" />

        </template>

    </div>
</template>
<script>
import { payment_type, trade_type } from "@/utils/payment-type";
import { mapGetters, mapState } from "vuex";
export default {
    name: "UserBillingTransaction",
    layout: "layout-user",
    data () {
        return {
            inited: false,
            loading: false,
            searching: false,
            tab: "order",
            tableList: [],
            form: {
                page_num: 1,
                page_size: 10,
                total: 0,
                order_code: "",
                trade_type: "",
                payment: "",
                time_s: "",
                time_e: "",
            },

        };
    },
    computed: {
        ...mapState('user', {
            is_wallet: 'is_wallet',
        }),
        tradeOptions () {
            const result = [
                { label: "Payment", value: 1 },
                { label: "Recharge", value: 3 },
                { label: "Automatic payment ", value: 4 },
                { label: "Refund", value: 2 },

            ]
            if (this.tab == 'wallet') {
                return result
                // return []
            } else {
                return result.filter(i => i.value != 3)
            }
        },
        paymentOptions () {

            const result = [
                { label: "Paypal", value: 1 },
                { label: "Credit card", value: 2 },
                // { label: "Debit card", value: 3 },
                // { label: "Wallet", value: 4 },
            ]

            if (this.is_wallet == 2) {
                result.push({ label: "Wallet", value: 4 })
            }

            return result
        }
    },
    methods: {
        //  请求数据
        async getData (tab) {
            let type = tab || this.tab;
            let api = type == "order" ? "Wallet.getTradeLog" : "Wallet.getWalletLog";
            let req = {
                page_num: this.form.page_num,
                page_size: this.form.page_size,
                order_code: (this.form.order_code || '').trim(),
                trade_type: this.form.trade_type,
                payment: this.form.payment,
                time_s: this.form.time_s,
                time_e: this.form.time_e,
            };
            this.loading = true;
            let res = await this.$http(api, req);

            if (res.code == 0) {
                this.tableList = res.data;
                this.form.total = res.total;
                this.loading = false;
                this.searching = req.order_code || req.trade_type || req.time_s || req.time_e || req.payment

            } else {
                this.loading = false;
                this.$uxMessage.warning(res.msg);
            }
        },

        async doSearch () {
            this.form.page_num = 1;
            await this.getData()
        },

        setTab (type) {
            if (this.tab != type) {
                this.form.page_num = 1;
                this.form.order_code = "";
                this.form.trade_type = "";
                this.form.payment = "";
                this.form.time_s = "";
                this.form.time_e = "";
                // this.$router.replace({ path: this.$route.path, query: { type: type } })
                // 清空数据
                this.getData(type).then(() => {
                    this.tab = type;
                })
            }
        },

        getStatusHtml (item) {

            const code = item.transaction_status || 0;
            if (code == 0 || code == 10) {
                return '<span style="color: #008552" >Completed</span>'
            } else if (code == 1) {
                return '<span style="color: #B88800" >Pending</span>'
            } else if (code == 2) {
                return '<span style="color: #C62828" >Delayed transaction</span>'
            } else if (code == 11) {
                return '<span style="color: #757575" >Refunding</span>'
            } else if (code == 20) {
                return '<span style="color: #C62828" >Failed</span>'
            }
        },

        getPriceFormat (value) {
            const val = parseInt(value)
            if (val) {
                return (val / 100).toFixed(2)
            } else {
                return '0.00'
            }
        },
        getPaymentById (id) {
            if (id != 0) {
                return payment_type[id];
            } else {
                return "consume";
            }
        },
        getTradeById (id) {
            return trade_type[id];
        },
        getTimeByDate (date) {
            let str = "";
            if (date) {
                let time = new Date(date).getTime();
                str += this.$timeFormat(time);
            } else {
                str = "- -";
            }
            return str;
        },

        handleCurrentChange (val) {
            this.form.page_num = val;
            this.getData();
        },
    },
    created () {
        if (process.client) {
            if (this.$route.query.type) {
                let type = this.$route.query.type;
                let tab = /order|wallet/.test(type) ? type : 'order';
                this.tab = tab;
            }
            this.getData().then(() => {
                this.inited = true
            });

        }
    },
};
</script>
<style lang="scss" scoped>
.transaction {
    display: flex;
    flex-direction: column;
    position: relative;

    &>.loading {
        position: fixed;
        left: 228px;
        top: 0;
        bottom: 0;
        right: 0;
    }

    &>.initing {
        flex: 1;
        width: 100%;
    }

    &>h2 {
        display: flex;
        align-items: center;
        font-size: 36px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 40px;
    }
}

.transaction-tab {
    display: flex;
    position: relative;
    margin-top: 28px;
    margin-right: auto;

    &::before {
        content: '';
        position: absolute;
        left: 0;
        bottom: 0;
        right: 0;
        height: 1px;
        background-color: #dcdcdc;
    }

    button {
        position: relative;
        border: none;
        line-height: 40px;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #555555;
        padding: 0;

        &::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            right: 0;
            height: 2px;
            border-radius: 2px;
            background-color: transparent;
        }

        &:hover {
            &::after {
                background-color: #f6b2cd;
            }
        }

        &.active {
            color: #ea4c89;

            &::after {
                background-color: #ea4c89;
            }
        }

        &+button {
            margin-left: 20px;
        }
    }
}

.transaction-form {
    display: flex;
    margin-top: 40px;
    height: 32px;

    .user-search-name {
        flex: 1;
        max-width: 230px;
    }

    .form-select {
        margin-left: 10px;
        flex: 1;
        max-width: 230px;
        height: 32px;
    }

    .el-select {
        margin-left: 10px;
        flex: 1;
        max-width: 230px;

        &::v-deep .el-input__inner {
            height: 32px;
            line-height: 32px;
            border-radius: 8px;
            border-color: #e6e6e6;

            &:hover {
                border-color: #c7c7c7;
            }

            &:focus {
                border-color: #f6b2cd;
                box-shadow: 0 0 1px 1px #f6b2cd;
            }
        }

        &::v-deep .el-input__icon {
            line-height: 32px;
        }
    }

    .mx-datepicker {
        margin-left: 10px;
        max-width: 230px;

        &::v-deep input {
            height: 32px;

            &:hover {
                border-color: #c7c7c7;
            }

            &:focus {
                border-color: #f6b2cd;
                box-shadow: 0 0 1px 1px #f6b2cd;
            }
        }
    }

    &>a {
        width: 96px;
        height: 32px;
        line-height: 30px;
        background: #ffffff;
        border-radius: 4px;
        border: 1px solid #d9d9d9;
        font-size: 14px;

        color: #000000;
        text-align: center;
    }

    &>strong {
        font-size: 12px;
        color: #555555;
        margin-left: 12px;
        height: 32px;
        line-height: 32px;
        line-height: 32px;
        font-weight: normal;
        display: flex;
        align-items: center;
        position: relative;

        i {
            font-size: 14px;
            margin-right: 4px;
            color: #acacac;
            height: 14px;
            line-height: 14px;
        }
    }
}

.transaction-table {
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #ebebeb;
    padding: 0 14px 14px;
    min-height: 543px;

    table {
        thead {
            th {
                min-height: 20px;
                font-size: 14px;
                font-family: Roboto-Medium, Roboto;
                font-weight: 500;
                color: #292929;
                line-height: 20px;
                position: relative;
                padding: 10px;

                &::before {
                    content: '';
                    position: absolute;
                    left: 0;
                    bottom: 0;
                    right: 0;
                    height: 1px;
                    background: #ebebeb;
                }

                &:first-child {
                    &::before {
                        left: -14px;
                    }
                }

                &:last-child {
                    &::before {
                        right: -14px;
                    }
                }

                &:nth-child(1) {
                    width: 164px;
                }

                &:nth-child(2) {
                    width: 178px;
                }

                &:nth-child(3) {
                    width: 180px;
                }
            }
        }

        tbody {
            td {
                text-align: center;
                padding: 16px 10px;
                font-size: 14px;
                font-family: Roboto-Regular, Roboto;
                font-weight: 400;
                color: #000000;
                line-height: 16px;

                &:first-child {
                    border-radius: 8px 0 0 8px;
                }

                &:last-child {
                    border-radius: 0 8px 8px 0;
                }
            }

            tr:nth-child(2n) {
                td {
                    background: #f8f8f8;
                }
            }
        }
    }
}

.table-nodata {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    svg {
        display: block;
        width: 40px;
        height: 40px;
        border: 3px solid #d8d8d8;
        border-radius: 40px;
        padding: 3px;
        color: #d8d8d8;
    }

    span {
        margin-top: 24px;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #9e9e9e;
        line-height: 20px;
    }

    .search-empty {
        border: none;
    }
}

.common-page {
    padding: 24px 0 0;
    justify-content: flex-end;
}
</style>
